import { IMetaInfo } from '@/types/map-file'
import { isNumber, isString } from 'lodash'
import * as proj from 'ol/proj'
function useMapHelper() {
    function getImgExtent(jsonobj: IMetaInfo) {
        const imgExtent = [
            Math.min(jsonobj.xmin, jsonobj.xmax),
            Math.min(jsonobj.ymin, jsonobj.ymax),
            Math.max(jsonobj.xmin, jsonobj.xmax),
            Math.max(jsonobj.ymin, jsonobj.ymax),
        ]
        return imgExtent
    }

    const buildPixelProjection = (imgExtent: number[]) => {
        const projection = new proj.Projection({
            code: 'xkcd-image',
            units: 'pixels',
            extent: imgExtent,
        })
        return projection
    }

    const replaceScpFolderBase = function (baseFolderUrl: string, url: string) {
        return url.replace(baseFolderUrl, '$[SCP_SHARE]/')
    }

    const translateFullProjectionCode = (code: number | string) => {
        if (isNumber(code)) {
            return `EPSG:${code}`
        } else if (isString(code)) {
            const str = code.search(/EPSG:/) > -1 ? code : `EPSG:${code}`

            return str
        }
    }

    return {
        getImgExtent,
        buildPixelProjection,
        replaceScpFolderBase,
        translateFullProjectionCode,
    }
}

export default useMapHelper
